<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\dao\car;


use app\dao\BaseDao;
use app\model\car\BasePlace;

/**
 * Class BasePlaceDao
 * @package app\dao\shendeng
 */
class BasePlaceDao extends BaseDao
{
    protected function setModel(): string
    {
        return BasePlace::class;
    }

    /**
     * 列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getList(array $where = [], int $page = 0,int $limit = 0){
        $model = $this->getModel()->alias('p')->where(['p.is_del'=>0]);
        $model = $model->field('p.*,c.city_name,b.title as store_name');
        $model = $model->join('base_city c','p.city_id=c.id','left')->join('base_place b','p.store_id=b.id','left');
        $model = $this->setSearch($model,$where);
        $list = $model->order('p.id desc')->page($page,$limit)->select()->toArray();
        $count = $model->count();
        return compact('list','count');
    }

    /**
     * 搜索条件
     * @param $model
     * @param $where
     * @return mixed
     */
    public function setSearch($model,$where){
        if(isset($where['keyword'])&&$where['keyword']){
            $model->where('p.name','like',"%{$where['keyword']}%");
        }
        if($where['status']!==''){
            $model->where('p.status','=',$where['status']);
        }
        if(isset($where['city_id'])&&$where['city_id']){
            $model->where('p.city_id','=',$where['city_id']);
        }
        if($where['place_type']!==''){
            $model->where('p.place_type','=',$where['place_type']);
        }
        if(isset($where['store_id'])&&$where['store_id']){
            $model->where('p.store_id','=',$where['store_id']);
        }
        if(isset($where['id'])&&$where['id']){
            $model->where('p.id','=',$where['id']);
        }
        return $model;
    }

    /**
     * 网点列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param bool $is_page
     * @return array
     */
    public function getOutletList(array $where,int $page,int $limit,bool $is_page){
        $model = $this->getModel()->alias('p')->where(['p.is_del'=>0]);
        $model = $model->field('p.*,b.title as store_name,c.city_name');
        $model = $model->join('base_place b','b.id=p.store_id','left')->join('base_city c','c.id=p.city_id','left');
        $model = $this->setSearch($model,$where);
        $count = $model->count();
        if($is_page) $model = $model->page($page,$limit);
        $list = $model->order('p.id desc')->select()->toArray();
        return compact('count','list');
    }
}
